package com.bocai.kelalacar.ui.car.set.presenter;

import android.util.Log;

import com.bocai.kelalacar.base.BaseResponse;
import com.bocai.kelalacar.bean.CarDriverBean;
import com.bocai.kelalacar.net.Api;
import com.bocai.kelalacar.ui.car.set.contract.AddDriverContract;
import com.bocai.kelalacar.util.AESUtils;
import com.bocai.kelalacar.util.RetryWithDelay;
import com.google.gson.Gson;

import java.util.List;

import javax.inject.Inject;

import rx.Subscriber;
import rx.android.schedulers.AndroidSchedulers;
import rx.schedulers.Schedulers;

/**
 * Created by yuanfei on 2017/6/27.
 */

public class AddDriverPresenter extends AddDriverContract.presenter {

    private final AddDriverContract.view view;
    private final Api api;
    public String start = "{\"content\":";
    public String end = "}";

    @Inject
    public AddDriverPresenter(AddDriverContract.view view, Api api) {
        this.view = view;
        this.api = api;
    }

    @Override
    public void addDriver(String memberId, String DriverName, String DriverPhone) {
        view.showLoading("");
        api.addDriver(memberId,DriverName,DriverPhone).subscribeOn(Schedulers.io()).retryWhen(new RetryWithDelay(3,3000))
                .observeOn(AndroidSchedulers.mainThread()).subscribe(new Subscriber<BaseResponse>() {
            @Override
            public void onCompleted() {
                view.stopLoading();
            }

            @Override
            public void onError(Throwable e) {
                Log.e("Throwable",e.toString());
                view.stopLoading();
                view.showErrorTip("网络异常");
            }

            @Override
            public void onNext(BaseResponse baseResponse) {
                view.stopLoading();
                Log.e("baseResponse",baseResponse.toString());
                Log.e("baseResponse",baseResponse.getReturnCode().equals("200")+"");
                if (baseResponse.getReturnCode().equals("200")){
                    view.addDriverSuccess();
                }else{
                    view.showErrorTip(baseResponse.getMsg());
                }
            }
        });
    }

    @Override
    public void getCarDriverList(String memberId) {
        api.getCarDriverList(memberId).subscribeOn(Schedulers.io()).retryWhen(new RetryWithDelay(3,3000))
                .observeOn(AndroidSchedulers.mainThread()).subscribe(new Subscriber<BaseResponse>() {
            @Override
            public void onCompleted() {
                view.stopLoading();
            }

            @Override
            public void onError(Throwable e) {
                Log.e("Throwable", e.toString());
                view.stopLoading();
                view.showErrorTip("网络异常");
            }

            @Override
            public void onNext(BaseResponse baseResponse) {
                view.stopLoading();
                Log.e("baseResponse", baseResponse.toString());
                Log.e("baseResponse", baseResponse.getReturnCode().equals("200") + "");
                if (baseResponse.getReturnCode().equals("200")) {
                    String result = start+ AESUtils.decode(baseResponse.getData())+end;
                    Log.e("result",result);
                    CarDriverBean bean = new Gson().fromJson(result,CarDriverBean.class);
                    view.getDriverList(bean);
                } else {
                    view.showErrorTip(baseResponse.getMsg());
                }
            }
        });
    }

    @Override
    public void deleteDriver(String driverId, final int index) {
        view.showLoading("");
        api.deleteDriver(driverId).subscribeOn(Schedulers.io()).retryWhen(new RetryWithDelay(3,3000))
                .observeOn(AndroidSchedulers.mainThread()).subscribe(new Subscriber<BaseResponse>() {
            @Override
            public void onCompleted() {
                view.stopLoading();
            }

            @Override
            public void onError(Throwable e) {
                Log.e("Throwable", e.toString());
                view.stopLoading();
                view.showErrorTip("网络异常");
            }

            @Override
            public void onNext(BaseResponse baseResponse) {
                view.stopLoading();
                Log.e("baseResponse", baseResponse.toString());
                Log.e("baseResponse", baseResponse.getReturnCode().equals("200") + "");
                if (baseResponse.getReturnCode().equals("200")) {
                    view.deleteDriverSuccess(index);
                } else {
                    view.showErrorTip(baseResponse.getMsg());
                }
            }
        });
    }

    @Override
    public void modifyDriver(String driverId, String DriverName, String DriverPhone) {
        view.showLoading("");
        api.modifyDriver(driverId,DriverName,DriverPhone).subscribeOn(Schedulers.io()).retryWhen(new RetryWithDelay(3,3000))
                .observeOn(AndroidSchedulers.mainThread()).subscribe(new Subscriber<BaseResponse>() {
            @Override
            public void onCompleted() {
                view.stopLoading();
            }

            @Override
            public void onError(Throwable e) {
                Log.e("Throwable", e.toString());
                view.stopLoading();
                view.showErrorTip("网络异常");
            }

            @Override
            public void onNext(BaseResponse baseResponse) {
                view.stopLoading();
                Log.e("baseResponse", baseResponse.toString());
                Log.e("baseResponse", baseResponse.getReturnCode().equals("200") + "");
                if (baseResponse.getReturnCode().equals("200")) {
                    view.modifyDriverSuccess();
                } else {
                    view.showErrorTip(baseResponse.getMsg());
                }
            }
        });
    }


}
